/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            //loading();
            this.event();
        },
        event: function () {
            this.data = {};
            var _this = this;
            $("#select_goods").on("click", function (e) {
                e.preventDefault();
                var type = $("input[name='type']:checked").val();
                _this.select_goods(site_id, type, e);
                $("#div_content_goods").show();
            });
            $("#btn-search").on("click", function (e) {
                e.preventDefault();
                var type = $("input[name='type']:checked").val();
                _this.select_goods(site_id, type, e);
            });
            $("#close_goods").on("click", function (e) {
                e.preventDefault();
                $("#div_content_goods").hide();
            });
            //保存
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });
            $("#btn_save").on("click", function () {
                _this.data.position = $("#position").val();
                if (_this.data.position == 0) {
                    Alert({msg: '请选择投放的位置', type: 'error'});
                    return false;
                }
                _this.data.type = $("input[name='type']:checked").val();
                _this.data.img = $("input[name='imgs']").val();
                if (_this.data.img == '' || _this.data.img == undefined) {
                    Alert({msg: '请传入一张图片', type: 'error'});
                    return false;
                }
                _this.data.goods_id = $('#goods_id').val();
                if (_this.data.goods_id == '' || _this.data.goods_id == undefined) {
                    Alert({msg: '请选择商品', type: 'error'});
                    return false;
                }
                _this.data.start_time = $("#dt_start").val();
                if (_this.data.start_time == '') {
                    Alert({msg: '请输入开始日期', type: 'error'});
                    return false;
                }
                _this.data.end_time = $("#dt_end").val();
                if (_this.data.end_time == '') {
                    Alert({msg: '请输入结束日期', type: 'error'});
                    return false;
                }
                //_this.data.activity_price = $("#activity-price").val();
                //if (_this.data.activity_price <= 0 || _this.data.activity_price > $('#price').val()) {
                //    Alert({msg: '活动价必须大于0且小于商品121价', type: 'error'});
                //    return false;
                //}
                _this.data.listorder = $("#listorder").val();
                new Model({
                    url: _global.url.api + 'apply_act',
                    data: _this.data
                }).post();
                location.href = '/site/apply_act'
            });
            $('body').on('click', '.item-bind', function (e) {
                e.preventDefault();
                $('#goods-item').html("");
                var goods_id = $(this).attr('attr');
                var goods_name = $(this).attr('all');
                var goods_price = $(this).attr('price');
                $('#goods-item').append(template('goods-item-tpl', {
                    goods_id: goods_id,
                    goods_name: goods_name,
                    goods_price: goods_price
                }));
                $("#div_goods").show();
                $('.goods-item-del').on('click', function (e) {
                    e.preventDefault();
                    $(this).parent().parent().remove();
                    $("#data-list").find('.item-bind').removeClass('bind_success').addClass('btn-default');

                });
                $(this).addClass('bind_success').removeClass('btn-default');
                // console.log($(this).parent().parent().siblings('tr').find('.item-bind'));
                $(this).parent().parent().siblings().find('.item-bind').removeClass('bind_success').addClass('btn-default');
            });
            $("input[name='type']").on("click", function () {
                if ($(this).val() == 3) {
                    $("#data-list").html('');
                    $(".group_buy").hide();
                    $("#div_content_goods").hide();
                    $("#goods-item").html('');
                } else {
                    $("#data-list").html('');
                    $(".group_buy").show();
                    $("#div_content_goods").hide();
                    $("#goods-item").html('');
                }
            });  //上传图片
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        },
        select_goods: function (site_id, type, e) {
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-items',
                url: _global.url.api + 'apply_goods',
                condition: {site_id: site_id, type: type, keyword: $("#keyword").val(), limit: 5},
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        }

    };


    new index();

});